package som;

import java.io.IOException;
import java.text.DecimalFormat;

public class Main {
	
	private static final double BUCKET_12_THRESHOLD = 1.0;
	private static final double BUCKET_23_THRESHOLD = 100.0;
	
	public static DecimalFormat df = new DecimalFormat("###.###");
	
	public static ForestFiresNetwork network = new ForestFiresNetwork(15, 15, 4);
	//public static ForestFiresNetwork1D network = new ForestFiresNetwork1D(40, 4);
	
	public static void main(String[] args) {
		
		network.run();
		
		/*
		//Print neuron w values.
		for(List<Neuron> list : network.inputLayer) {
			for(Neuron n : list) {
				System.out.print("[");
				List<Double> synapse = n.getSynapse();
				for(int i=0; i<11-1; i++) {
					System.out.print(df.format(synapse.get(i)));
					System.out.print("|");
				}
				System.out.print(df.format(synapse.get(10)));
				System.out.print("] ");
			}
			System.out.println("\n");
		}
		*/
		
		//network.runUMatrix();
		
		//network.runCategorization();
		
		network.runContinuousCategorization();
		
		/*
		
		//NOMINAL CLASSIFICATION
		
		int count1 = 0;
		int count2 = 0;
		int count3 = 0;
		int total1 = 0;
		int total2 = 0;
		int total3 = 0;
		
		for(int i=0; i<network.dataSet.getRowSize(); i++) {
			boolean result = network.assertCorrectness(i);
			
			double fire_area = Double.parseDouble(network.dataSet.getValue(i, 12));
			
			if(fire_area < BUCKET_12_THRESHOLD) {
				total1++;
				if(result)
					count1++;
			}
			else if(fire_area < BUCKET_23_THRESHOLD) {
				total2++;
				if(result)
					count2++;
			}
			else {
				total3++;
				if(result)
					count3++;
			}
		}
		
		System.out.println("\n\nCorrectly classified instances (class 1 - \"no fire\"): " + 1.0d*count1/total1 + " (" + count1 + "/" + total1 +")");
		System.out.println("\n\nCorrectly classified instances (class 2 - \"small fire\"): " + 1.0d*count2/total2 + " (" + count2 + "/" + total2 +")");
		System.out.println("\n\nCorrectly classified instances (class 3 - \"large fire\"): " + 1.0d*count3/total3 + " (" + count3 + "/" + total3 +")");
		*/
		
		//CONTINUOUS CLASSIFICATION
		
		int count1 = 0;
		int count2 = 0;
		int count3 = 0;
		int total1 = 0;
		int total2 = 0;
		int total3 = 0;
		
		for(int i=0; i<network.dataSet.getRowSize(); i++) {
			double error = network.assertError(i);
			
			double fire_area = Double.parseDouble(network.dataSet.getValue(i, 12));
			
			if(fire_area < BUCKET_12_THRESHOLD) {
				total1++;
				if(error < ForestFiresNetwork.ACCEPTABLE_ERROR)
					count1++;
			}
			else if(fire_area < BUCKET_23_THRESHOLD) {
				total2++;
				if(error < ForestFiresNetwork.ACCEPTABLE_ERROR)
					count2++;
			}
			else {
				total3++;
				if(error < ForestFiresNetwork.ACCEPTABLE_ERROR)
					count3++;
			}
		}
		
		System.out.println("\n\nCorrectly classified instances (class 1 - \"no fire\"): " + 1.0d*count1/total1 + " (" + count1 + "/" + total1 +")");
		System.out.println("\n\nCorrectly classified instances (class 2 - \"small fire\"): " + 1.0d*count2/total2 + " (" + count2 + "/" + total2 +")");
		System.out.println("\n\nCorrectly classified instances (class 3 - \"large fire\"): " + 1.0d*count3/total3 + " (" + count3 + "/" + total3 +")");
		
		
		System.out.println("\nPress Enter to exit...");
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
